# ========================================================== # Clear Microsoft Teams Cache (Device Specific) # Supports: New Teams # Clears cache for ALL user profiles in this device # ========================================================== Write-Host "=============================================" Write-Host " Microsoft Teams Cache Cleanup Started" Write-Host "=============================================" # ---------------------------------------------------------- # Stop Teams Processes # ---------------------------------------------------------- Write-Host "`n[INFO] Closing Microsoft Teams..." # Get Teams process $teamsProcess = Get-CimInstance Win32_Process | Where-Object { $_.Name -eq "ms-teams.exe" } if ($teamsProcess) { Write-Host "`n[INFO] Teams process found" # Get child processes of Teams $childProcesses = Get-CimInstance Win32_Process | Where-Object { $_.ParentProcessId -eq $teamsProcess.ProcessId } foreach ($child in $childProcesses) { Write-Host "[INFO] Stopping Child Process: $($child.Name)" try { Stop-Process -Id $child.ProcessId -Force -ErrorAction Stop Write-Host "[SUCCESS] Stopped: $($child.Name)" } catch { Write-Host "[ERROR] Failed: $($child.Name)" } } # Stop main Teams process try { Stop-Process -Id $teamsProcess.ProcessId -Force -ErrorAction Stop Write-Host "`n[SUCCESS] Microsoft Teams stopped" } catch { Write-Host "`n[ERROR] Failed to stop Teams" } } else { Write-Host "`n[INFO] Microsoft Teams is not running" } Start-Sleep -Seconds 3 # ---------------------------------------------------------- # Get All User Profiles # ---------------------------------------------------------- $ExcludedUsers = @( "Public", "Default", "Default User", "All Users" ) # Add users passed through script arguments for ($i = 0; $i -lt $args.Count; $i++) { if (![string]::IsNullOrWhiteSpace($args[$i])) { $ExcludedUsers += $args[$i] } } # Get profiles excluding specified users $UserProfiles = Get-ChildItem "C:\Users" -Directory | Where-Object { $_.Name -notin $ExcludedUsers } if ($UserProfiles.Count -eq 0) { Write-Host "[ERROR] No user profiles found." exit } # ---------------------------------------------------------- # Function to Clear Folder Contents # ---------------------------------------------------------- function Clear-TeamsCache { param ( [string[]]$Paths ) foreach ($path in $Paths) { if (Test-Path $path) { $item = Get-Item $path -Force -ErrorAction SilentlyContinue if ($item.Attributes -band [System.IO.FileAttributes]::ReparsePoint) { Write-Host "[SKIPPED] Reparse point (symlink/junction): $path" continue } try { Write-Host "[INFO] Clearing: $path" Get-ChildItem -Path $path -Force -ErrorAction SilentlyContinue | Where-Object { -not ($_.Attributes -band [System.IO.FileAttributes]::ReparsePoint) } | Remove-Item -Recurse -Force -ErrorAction SilentlyContinue Write-Host "[SUCCESS] Cleared" } catch { Write-Host "[ERROR] Failed to clear: $path" } } else { Write-Host "[INFO] Path not found: $path" } } } # ---------------------------------------------------------- # Clear Teams Cache For Each User # ---------------------------------------------------------- foreach ($profile in $UserProfiles) { Write-Host "`n============================================" Write-Host " Processing User: $($profile.Name)" Write-Host "=============================================" $basePath = $profile.FullName $newTeamsPaths = @( "$basePath\AppData\Local\Packages\MSTeams_8wekyb3d8bbwe\LocalCache", "$basePath\AppData\Local\Packages\MSTeams_8wekyb3d8bbwe\LocalState", "$basePath\AppData\Local\Packages\MSTeams_8wekyb3d8bbwe\TempState", "$basePath\AppData\Local\Microsoft\Teams\Cache", "$basePath\AppData\Local\Microsoft\Teams\GPUCache", "$basePath\AppData\Local\Microsoft\Teams\IndexedDB", "$basePath\AppData\Local\Microsoft\Teams\Local Storage", "$basePath\AppData\Local\Microsoft\Teams\tmp" ) Clear-TeamsCache -Paths $newTeamsPaths } # ---------------------------------------------------------- # Restart Teams (Optional) # ---------------------------------------------------------- try { Write-Host "`n[INFO] Restarting Microsoft Teams..." Start-Process "ms-teams.exe" Write-Host "[SUCCESS] Microsoft Teams restarted." } catch { Write-Host "[ERROR] Microsoft Teams executable not found." Write-Host "[INFO] Please start Microsoft Teams manually." } # ---------------------------------------------------------- # Completion Message # ---------------------------------------------------------- Write-Host "`n=============================================" Write-Host " Microsoft Teams Cache Cleanup Completed" Write-Host "============================================="